package com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor;

import java.sql.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.qqInterfaceCall.QQInterfaceCall;
import com.romotc.scache.RequestSpy;
/**
 * 一次登录结果处理
 * @author romotc
 *
 */
public class Ptlogin2_Login_Processor implements DateProcessor {
	/**
	 * 一次登录是否正确：boolean true or false
	 */
	private static final String Key_FirstLoginSuccess = "Ptlogin2_Login_Processor_LoginSuccess";
	/**
	 * 一次登录结果提示信息
	 */
	public static final String Key_FirstLoginResultInfo = "Ptlogin2_Login_Processor_LoginResultInfo";
	/**
	 * 登录结果 整数
	 */
	public static final String Key_FirstLoginResult = "Ptlogin2_Login_Processor_LoginResult";
	
	
	private static Ptlogin2_Login_Processor instance;
	public synchronized static Ptlogin2_Login_Processor getInstance() {
		if(instance == null)
			instance = new Ptlogin2_Login_Processor();
		
		return instance;
	}
	

	@Override
	public JSONObject processResponse(QQLogedin loginQQ, RequestSpy rqSpy,
			JSONObject reqDataObj) throws Exception {
		//ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=30','0','登录成功！' 					  , 'Romotc');
		//ptuiCB('3','0',''															 ,'0','您输入的帐号或者密码不正确，请重新输入。', '97190867');
		String resbody = rqSpy.getResBody();
		
		String[] resData = parseData(resbody);
		if(resData != null && resData.length >= 6) {
			//登录是否正确
			reqDataObj.put(QQInterfaceCall.Key_CallResult, 
					WebQQConstant.Result_Key_PTLOGIN2_LOGIN_Success.equals(resData[0]) ? QQInterfaceCall.Key_CallResult_Success : QQInterfaceCall.Key_CallResult_Error);
			
			reqDataObj.put(Key_FirstLoginResult, resData[0]);
			reqDataObj.put(Key_FirstLoginResultInfo, resData[4]);
		}else {
			//登录是否正确
			reqDataObj.put(QQInterfaceCall.Key_CallResult, QQInterfaceCall.Key_CallResult_Error);
			reqDataObj.put(Key_FirstLoginResultInfo, "接口返回信息解析失败:" + resbody);
		}
		
		
		return reqDataObj;
	}

	private String[] parseData(String resbody) {
		Pattern pattern = Pattern.compile(WebQQConstant.Result_Regex_PTLOGIN2_LOGIN);
		Matcher m = pattern.matcher(resbody);
		if(m.matches()) {
			String data1 = m.group(1);
			String data2 = m.group(2);
			String data3 = m.group(3);
			String data4 = m.group(4);
			String data5 = m.group(5);
			String data6 = m.group(6);
			
			String[] data = new String[]{data1, data2, data3, data4, data5, data6};
			return data;
		}
		
		return null;
	}
	
	public static void main(String[] args) {
		String resbody = "ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=30','0','登录成功！', 'Romotc');";
		resbody = "ptuiCB('3','0','','0','您输入的帐号或者密码不正确，请重新输入。', '97190867');";
		resbody = "ptuiCB('7','0','','0','很遗憾，网络连接出现异常，请您稍后再试。(2384462424)', '97190867');\r\n";
		String[] data = Ptlogin2_Login_Processor.getInstance().parseData(resbody);
		if(data != null && data.length >= 6) {
			for(String dataStr : data) {
				System.out.println(dataStr);
			}
		}
	}



}
